package net.wanho.shop.service.impl;

import net.wanho.shop.po.GoodsPics;
import net.wanho.shop.mapper.GoodsPicsMapper;
import net.wanho.shop.service.GoodsPicsService;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 商品图片表 服务实现类
 * </p>
 *
 * @author 猛虎王
 * @since 2024-12-24
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class GoodsPicsServiceImpl implements GoodsPicsService {

    private final GoodsPicsMapper goodsPicsMapper;

    /**
     * 商品图片表分页查询
     * @param pageNum
     * @param pageSize
     * @param goodsPics
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo findPage(int pageNum, int pageSize, GoodsPics goodsPics) {
        if(ObjectUtil.isNull(goodsPics)){
            goodsPics = new GoodsPics();
        }

        // 分页
        PageHelper.startPage(pageNum,pageSize);
        List<GoodsPics> goodsPicsList = goodsPicsMapper.select(goodsPics);
        return new PageInfo(goodsPicsList);
    }

    /**
     * 根据主键查询商品图片表
     * @param picsId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public GoodsPics findById(Serializable picsId) {
        return goodsPicsMapper.selectById(picsId);
    }

    /**
     * 商品图片表新增
     * @param goodsPics
     */
    @Override
    public void add(GoodsPics goodsPics) {
        goodsPicsMapper.insert(goodsPics);
    }

    /**
     * 修改商品图片表
     * @param goodsPics
     */
    @Override
    public void modify(GoodsPics goodsPics) {
        goodsPicsMapper.update(goodsPics);
    }

    /**
     * 根据主键删除商品图片表
     * @param picsId
     */
    @Override
    public void removeById(Serializable picsId) {
        if(((String)picsId).contains(",")){
            goodsPicsMapper.deleteByIds(((String)picsId).split(","));
        }else {
            goodsPicsMapper.deleteById(picsId);
        }
    }
}
